package cn.itcast.service.cargo.impl;

import cn.itcast.dao.cargo.ContractDao;
import cn.itcast.dao.cargo.ExtCproductDao;
import cn.itcast.domain.cargo.Contract;
import cn.itcast.domain.cargo.ExtCproduct;
import cn.itcast.domain.cargo.ExtCproductExample;
import cn.itcast.service.cargo.ExtCproductService;
import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;
import java.util.UUID;

@Service
public class ExtCproductServiceImpl implements ExtCproductService {

	@Autowired
	private ExtCproductDao extCproductDao;

	@Autowired
	private ContractDao contractDao;


	/**
	 * 保存
	 *  参数：extCproduct （附件）
	 *      包含 contractId 合同
	 *      包含 contractProductId 所属货物id
	 */
	public void save(ExtCproduct ext) {
		//1.计算保存附件的总金额
		double money = 0.0;
		if(ext.getCnumber() != null && ext.getPrice() != null) {
			money = ext.getCnumber() * ext.getPrice();
		}
		ext.setAmount(money);
		//2.设置附件的id
		ext.setId(UUID.randomUUID().toString());
		//3.保存附件
		extCproductDao.insertSelective(ext);
		//4.根据合同id查询合同
		Contract contract = contractDao.selectByPrimaryKey(ext.getContractId());
		//5.设置合同总金额
		contract.setTotalAmount(contract.getTotalAmount() + money);
		//6.设置合同附件批次数量+1
		contract.setExtNum(contract.getExtNum() + 1);
		//7.更新合同
		contractDao.updateByPrimaryKeySelective(contract);
	}


	/**
	 * 更新
	 * 参数：extCproduct （附件）
	 *     包含 contractId 合同
	 *     包含 contractProductId 所属货物id
	 */
	public void update(ExtCproduct newExt) {
		//1.计算更新之后附件金额
		double newMoney = 0.0;
		if(newExt.getCnumber() != null && newExt.getPrice() != null) {
			newMoney = newExt.getCnumber() * newExt.getPrice();
		}
		newExt.setAmount(newMoney);
		//2.查询更新之前附件金额
		ExtCproduct oldExt = extCproductDao.selectByPrimaryKey(newExt.getId());
		Double oldMoney = oldExt.getAmount();
		//3.更新附件
		extCproductDao.updateByPrimaryKeySelective(newExt);
		//4.根据id查询购销合同
		Contract contract = contractDao.selectByPrimaryKey(newExt.getContractId());
		//5.设置合同的总金额
		contract.setTotalAmount(contract.getTotalAmount() - oldMoney + newMoney);
		//6.更新购销合同
		contractDao.updateByPrimaryKeySelective(contract);
	}

	/**
	 * 删除附件
	 *  参数：附件ID
	 */
	public void delete(String id) {
		//1.根据id查询附件（获取删除的金额）
		ExtCproduct ext = extCproductDao.selectByPrimaryKey(id);
		Double money = ext.getAmount();
		//2.删除附件
		extCproductDao.deleteByPrimaryKey(id);
		//3.根据id查询购销合同
		Contract contract = contractDao.selectByPrimaryKey(ext.getContractId());
		//4.设置购销合同总金额
		contract.setTotalAmount(contract.getTotalAmount() - money);
		contract.setExtNum(contract.getExtNum() - 1);
		//5.更新购销合同
		contractDao.updateByPrimaryKeySelective(contract);
	}

	@Override
	public ExtCproduct findById(String id) {
		return extCproductDao.selectByPrimaryKey(id);
	}

	@Override
	public PageInfo findAll(ExtCproductExample example, int page, int size) {
		PageHelper.startPage(page,size);
		List<ExtCproduct> list = extCproductDao.selectByExample(example);
		return new PageInfo(list);
	}
}
